#!/bin/bash

# 固定的根目录
ROOT_DIR="/home/hspay/service"

# 服务名和对应的两个端口号的数组
SERVICES=(
    "xxpay-agent 8203 8303 /www/server/panel/vhost/nginx/proxy/agent.newmoneypaye.com"
    "xxpay-task 8205 8205 /www/server/panel/vhost/nginx/proxy/task.newmoneypaye.com"
    "xxpay-isv 8206 8306 /www/server/panel/vhost/nginx/proxy/isv.newmoneypaye.com"
    "xxpay-pay 8201 8301 /www/server/panel/vhost/nginx/proxy/pay.newmoneypaye.com"
    "xxpay-merchant 8204 8304 /www/server/panel/vhost/nginx/proxy/mch.newmoneypaye.com"
)

# 指定服务目录
SERVICE_DIR=""
SERVICE_JAR=""
SERVICE_NAME=""
NGINX_CONF=""

# 指定端口号，默认为8080
PORT=8080
DEFAULT_PORT=0
BACKUP_PORT=0

# 根据命令行参数设置服务目录和端口号
if [ $# -lt 2 ]; then
    echo "Usage: $0 {start|stop|restart} [service_name]"
    exit 1
fi

# 根据命令行参数设置服务目录
SERVICE_NAME="$2"
for SERVICE in "${SERVICES[@]}"; do
    service_parts=($SERVICE)
    SERVICE_NAME_IN_ARRAY="${service_parts[0]}"
    if [ "$SERVICE_NAME_IN_ARRAY" = "$SERVICE_NAME" ]; then
        SERVICE_DIR="$ROOT_DIR/$SERVICE_NAME"
        SERVICE_JAR="$SERVICE_NAME.jar"
        SERVICE_NAME="$SERVICE_NAME"
        DEFAULT_PORT="${service_parts[1]}"  # 从数组元素中提取第一个端口号
        BACKUP_PORT="${service_parts[2]}"  # 从数组元素中提取第二个端口号
        NGINX_CONF="${service_parts[3]}"

        # 检查默认端口是否在监听，如果没有，使用默认端口
        if ! netstat -tuln | grep ":$DEFAULT_PORT" > /dev/null; then
            PORT=$DEFAULT_PORT
            DEFAULT_PORT=$BACKUP_PORT
        else
            PORT=$BACKUP_PORT
            BACKUP_PORT=$DEFAULT_PORT
        fi

        break
    fi
done

if [ "$DEFAULT_PORT" -eq 0 ]; then
    echo "Unknown service name: $SERVICE_NAME"
    exit 1
fi

start() {
    echo "Starting the service $SERVICE_NAME on port $PORT..."
    nohup java -jar "$SERVICE_DIR/$SERVICE_JAR" --server.port=$PORT > /dev/null 2>&1 &
}

stop() {
    echo "Stopping the service $SERVICE_NAME on port $DEFAULT_PORT..."
    pkill -f "java -jar $SERVICE_DIR/$SERVICE_JAR --server.port=$DEFAULT_PORT"
}

chose_nginx_config() {
    if [ "$PORT" -ne 8205 ]; then
        mv "$NGINX_CONF/$PORT.conf.bak" "$NGINX_CONF/$PORT.conf"
        mv "$NGINX_CONF/$DEFAULT_PORT.conf" "$NGINX_CONF/$DEFAULT_PORT.conf.bak"
        nginx -s reload
        echo "nginx 切换配置成功"
    else
        # 在这里添加 DEFAULT_PORT 等于 8205 时执行的操作
        echo "DEFAULT_PORT is equal to 8205"
    fi
}

restart() {
    stop
    if [ $PORT -eq $DEFAULT_PORT ]; then
        PORT=$BACKUP_PORT
    else
        PORT=$DEFAULT_PORT
    fi
    start
}

check_port() {
    if netstat -tuln | grep ":$PORT" > /dev/null; then
        echo "Service $SERVICE_NAME is running, port $PORT is in listening state."
        chose_nginx_config
        sleep 30
        stop
        exit 0
    else
        echo "Service $SERVICE_NAME is not running, port $PORT is not in listening state."
    fi
}

case "$1" in
    start)
        start
        while true; do
            check_port
            sleep 5  # 每5秒检测一次
        done
        ;;
    stop)
        stop
        ;;
    restart)
        restart
        while true; do
            check_port
            sleep 5  # 每5秒检测一次
        done
        ;;
    *)
        echo "Usage: $0 {start|stop|restart} [service_name]"
        exit 1
        ;;
esac

exit 0